package plugins.lxfa.chengshiguangying
{
    import core.manager.*;
    import core.manager.playerManager.musicPlayerManager.BackGroundMusicManager;
    import core.manager.pluginManager.*;
    import core.manager.scriptManager.*;
    
    import flash.events.*;
    
    import memory.*;
    
    import mx.core.*;
    
    import plugins.lsd.CustomWindow.*;
    import plugins.lxfa.animatePlayer.AnimatePlayer;
    import plugins.model.*;

    public class ChengShiGuangYingBase extends UIComponent
    {
        private var customWindow:CustomWindow;
        private var itemModel:ItemModel;
        private var ID:int;
        private var flatWall3D_Reflection:FlatWall3D_Reflection;

        public function ChengShiGuangYingBase()
        {
            ScriptManager.getInstance().addApi(ScriptName.SHOW_CHENG_SHI_GUANG_YING, init);
            return;
        }// end function

        private function init3DView() : void
        {
            flatWall3D_Reflection = new FlatWall3D_Reflection(ID);
            flatWall3D_Reflection.x = 14;
            flatWall3D_Reflection.y = 80;
            flatWall3D_Reflection.addEventListener(Event.ADDED_TO_STAGE, onAdded);
            customWindow.addChild(flatWall3D_Reflection);
            return;
        }// end function

        private function onAdded(tip:Event) : void
        {
            customWindow.addEventListener(CustomWindowEvent.WINDOW_CLOSE, on_customWindow_close);
            MemoryRecovery.getInstance().gcFun(flatWall3D_Reflection, Event.ADDED_TO_STAGE, onAdded);
            return;
        }// end function

        private function on_swf_complete(tip:CustomWindowEvent) : void
        {
            this.addChild(customWindow);
            init3DView();
            return;
        }// end function

        private function init(tip:int) : void
        {
            this.ID = tip;
            BackGroundMusicManager.getInstance().soundStop();
            ScriptManager.getInstance().runScriptByName(ScriptName.STOP_RENDER, []);
            itemModel = new ItemModel();
            AnimatePlayer.getInstance().showAnimate(itemModel.getAnimateId(tip));
            customWindow = new CustomWindow(itemModel.getSwfUrl(tip), itemModel.getText(tip));
            customWindow.addEventListener(CustomWindowEvent.SWF_COMPLETE, on_swf_complete);
            return;
        }// end function

        private function on_customWindow_close(tip:CustomWindowEvent) : void
        {
            PluginManager.getInstance().removePluginById("ChengShiGuangYingModule");
            return;
        }// end function

        public function dispose(tip:CustomWindowEvent) : void
        {
            if (!MainSystem.getInstance().isBusy)
            {
                ScriptManager.getInstance().removeApi(ScriptName.SHOW_CHENG_SHI_GUANG_YING);
                if (flatWall3D_Reflection != null)
                {
                    if (flatWall3D_Reflection.parent != null)
                    {
                        flatWall3D_Reflection.parent.removeChild(flatWall3D_Reflection);
                    }
                    flatWall3D_Reflection.dispose();
                    flatWall3D_Reflection = null;
                }
                MemoryRecovery.getInstance().gcFun(customWindow, CustomWindowEvent.SWF_COMPLETE, on_swf_complete);
                MemoryRecovery.getInstance().gcFun(customWindow, CustomWindowEvent.WINDOW_CLOSE, on_customWindow_close);
                if (customWindow != null)
                {
                    if (customWindow.parent != null)
                    {
                        customWindow.parent.removeChild(customWindow);
                    }
                    customWindow.dispose();
                    customWindow = null;
                }
                 AnimatePlayer.getInstance().removeAnimate();
                MyGC.gc();
                BackGroundMusicManager.getInstance().reload();
            }
            return;
        }// end function

    }
}
